package com.market.product.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.market.category.dao.CategoryDao;
import com.market.category.domain.CategoryCommand;
import com.market.product.dao.ProductDao;
import com.market.product.domain.ProductCommand;
import com.market.product.validator.ProductValidator;
import com.market.util.FileUtil;

@Controller
public class ProWriteController {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private CategoryDao categoryDao;
	
	@ModelAttribute("command")
	public ProductCommand initCommand(){
		return new ProductCommand();
	}
	
	@ModelAttribute("outer")
	public List<CategoryCommand> initMarket(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("keyWord", "outer");		
		List<CategoryCommand> outer = categoryDao.list(map);		
		return outer;
	}
	
	@RequestMapping("/productWrite.do")
	public ModelAndView form(@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(searchMarket.equals("all"))
			map.put("keyWord", "outer");
		else
			map.put("keyWord", searchMarket);
		
		List<CategoryCommand> defCategory = categoryDao.list(map);
		
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("productWrite");
		mav.addObject("pageNum", pageNum);
		mav.addObject("searchMarket", searchMarket);
		mav.addObject("searchCategory", searchCategory);
		mav.addObject("defCategory", defCategory);
		return mav;
	}	
	
	@RequestMapping("/setCategories.do")
	public ModelAndView setCategories(@RequestParam("market")String market) throws Throwable{		
		Map<String, Object> map = new HashMap<String, Object>();		

		map.put("keyWord", market);
		List<CategoryCommand> list = categoryDao.list(map);
						
		ObjectMapper mapper = new ObjectMapper();
		String jsonData = mapper.writeValueAsString(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("categoriesJSON");
		mav.addObject("jsonData", jsonData);
		return mav;
	}	
	
	@RequestMapping(value="/productWrite.do",method=RequestMethod.POST)
	public String submit(@ModelAttribute("command")ProductCommand productCommand,
			BindingResult result,
			@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){
		
		new ProductValidator().validate(productCommand, result);
		
		if(result.hasErrors()){
			return "productWrite";
		}
		
		try{
			List<MultipartFile> list = productCommand.getUpload();
			String newNames = "";
			
			if(!list.get(0).isEmpty()){
				for(int i=0;i<list.size();i++){
					if(i>0)
						newNames += "&";
					newNames += i + FileUtil.rename(list.get(i).getOriginalFilename());
				}				
			}
			
			productCommand.setImage(newNames);
			productDao.insert(productCommand);	
			
			String newName[] = newNames.split("&");
			
			if(!list.isEmpty()){
				for(int i=0;i<list.size();i++){
					File file = new File(FileUtil.UPLOAD_PATH+"/" + newName[i]);
					list.get(i).transferTo(file);
				}								
			}			
		}catch(IOException e){
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}		
		return "redirect:/productList.do?pageNum="+pageNum+"&searchMarket="+searchMarket+"&searchCategory="+searchCategory;
	}
}
